/*
*	This file contains code for parsing the line and storing it in the ins structure
*	Every instruction fetch will execute all the functions in this file.. Atleast for those present now..
*
*	Author : Saurin ( saurin.shah.11@gmsil.com )
*	Date : Feb 14, 2012 - Valentines Day :)
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"

int get_insmnemonic(const char *, char *);
int get_insaddress(const char *, char *);

char* position1(const char *line, int value)
{
	if(value == 1)
		return (char *)strstr(line,DISASSEMBLE);
	else if(value == 2)
		return (char *)strstr(line,ADDRESS);
	else if(value == 3)
		return (char *)strstr(line,READREGMAX);
	else if(value == 4)
		return (char *)strstr(line,WRITEREGMAX);
	else if(value == 5)
		return (char *)strstr(line,MEMCOUNT);
}

int position2( int value)
{
	if(value == 1)
		return DIS_LENGTH;
	else if(value == 2)
		return ADDRESS_LENGTH;
	else if(value == 3)
		return READREGMAX_LENGTH;
	else if(value == 4)
		return WRITEREGMAX_LENGTH;
	else if(value == 5)
		return MEMCOUNT_LENGTH;
}

// 4 - read, 5 - write, 0 - not read/write
int get_readwritefromins(const char *line, char *mnem)
{
	char *mnemonic = NULL;
	mnemonic = strstr(line,READ_CHECK);
	if (mnemonic == NULL)
	{
		mnemonic = strstr(line,WRITE_CHECK);
	}
	else
	{
		mnemonic += READ_LENGTH;
		while ( *mnemonic != '\n' && *mnemonic != '\0' && *mnemonic != ' ')
		{
			*mnem++ = *mnemonic++;
		}
		*mnem = '\0';
		return 1; //Read
	}
	if (mnemonic != NULL)
	{
		mnemonic += WRITE_LENGTH;
		while ( *mnemonic != '\n' && *mnemonic != '\0' && *mnemonic != ' ')
			*mnem++ = *mnemonic++;
		*mnem = '\0';
		return 2; // write
	}
	return 0;
}


//Function takes the instruction line and returns the mnemonic in the instruction..
int get_fromins(const char *line, char *mnem, int choose )
{
	char *mnemonic = NULL;
	if( (mnemonic=(position1(line,choose))) == NULL )
		return -1;
	mnemonic += position2(choose);
	while( (*mnemonic != ' ' ) && (*mnemonic != '\0'))
		*mnem++ = *mnemonic++;
	*mnem = '\0';
	return 0;	
}

int get_insfromins(const char *line, char *mnem )
{
	char *mnemonic = NULL;
	if( (mnemonic=strstr(line,DISASSEMBLE)) == NULL )
		return -1;
	mnemonic += DIS_LENGTH;
	while(*mnemonic != '\0')
		*mnem++ = *mnemonic++;
	*mnem = '\0';
	return 0;	
}

void get_addpresent(char *op, int *add)
{
	*add = 0;
	while (*op != '\0')
	{
		if(*op == '+')
			*add += 1;
		*op++;
	}
}
void get_mulpresent(char *op, int *mul)
{
	*mul = 0;
	while (*op != '\0')
	{
		if(*op == '+')
			*mul += 1;
		*op++;
	}
}

int get_insbreakdown(const char *line, char *mnem, char *op1, char *op2, char *op3, char *op4, char *op5) 
{
	strcpy(mnem,"");
	strcpy(op1,"");
	strcpy(op2,"");
	strcpy(op3,"");
	strcpy(op4,"");
	strcpy(op5,"");
	while( (*line != ' ' ) && (*line != '\0'))
		*mnem++ = *line++;
	*mnem = '\0';
	if( *line == '\0')
		return 0;
	*line++;
	while( (*line != ',' ) && ( *line != '\0') )
	{
		*op1++ = *line++;
	}
	*op1 = '\0';
	if( *line == '\0')
		return 0;
	line += 2;
	while( (*line != ',') && (*line != '\0') )
	{
		*op2++ = *line++;
	}
	*op2 = '\0';
	if( *line == '\0')
		return 0;
	while( (*line != ',') && (*line != '\0') )
	{
		*op3++ = *line++;
	}
	*op3 = '\0';
	if( *line == '\0')
		return 0;
	while( (*line != ',') && (*line != '\0') )
	{
		*op4++ = *line++;
	}
	*op4 = '\0';
	if( *line == '\0')
		return 0;
	while( (*line != ',') && (*line != '\0') )
	{
		*op5++ = *line++;
	}
	*op5 = '\0';
	if( *line == '\0')
		return 0;
	return 0;	
}

static void regstore(int c, char **op1, char **creg1, char **creg2, char **creg3, char **creg4, char **creg5, char **creg6, char **creg7, char **creg8)
{
	if(c == 1 )
		*creg1++ = *op1;
	else if(c == 2)
		*creg2++ = *op1;
	else if(c == 3)
		*creg3++ = *op1;
	else if(c == 4)
		*creg4++ = *op1;
	else if(c == 5)
		*creg5++ = *op1;
	else if(c == 6)
		*creg6++ = *op1;
	else if(c == 7)
		*creg7++ = *op1;
	else if(c == 8)
		*creg8++ = *op1;
	else
		printf("Error in reg breakdown\n");
}


int get_insreg(char *mnem, char *op1, char *op2, char *op3, char *op4, char *op5, char *creg1, int *reg1, char *creg2, int *reg2, char *creg3, int *reg3, char *creg4, int *reg4, char *creg5, int *reg5, char *creg6, int *reg6, char *creg7, int *reg7, char *creg8, int *reg8)
{
	char *addr = NULL;
	int ptr = 0;
	int imm = 0;
	int c = 1;

	if( strlen(op1) > 0 )
	{

	if( (addr = strstr(op1,DWORDPTR)) != NULL )
		ptr = 1;
	else if( (addr = strstr(op1,BYTEPTR)) != NULL )
		ptr = 2;
	else if( (addr = strstr(op1,PTR)) != NULL ) 
		ptr = 3;

	//No ptr, no indirect memory reference. 
	if(ptr == 0)
	{
		int entered = 0;
		while( *op1 != '\0' )
		{
			entered = 1;
			if(imm != 1)
			{
				if(*op1 == '0' || *op1 == '1' || *op1 == '2' || *op1 == '3' || *op1 == '4' || *op1 == '5' || *op1 == '6' || *op1 == '7' || *op1 == '8' || *op1 == '9')
					imm = 1;
				else
				{
					if(*op1 != ' ')
					{
						if(c == 1 )
							*creg1++ = *op1;
						else if(c == 2)
							*creg2++ = *op1;
						else if(c == 3)
							*creg3++ = *op1;
						else if(c == 4)
							*creg4++ = *op1;
						else if(c == 5)
							*creg5++ = *op1;
						else if(c == 6)
							*creg6++ = *op1;
						else if(c == 7)
							*creg7++ = *op1;
						else if(c == 8)
							*creg8++ = *op1;
						else
							printf("Error in reg breakdown\n");
					}
					else
					{
						if(c == 1)
						{
							*reg1 = 1;
							*creg1 = '\0';
						}
						else if(c == 2)
						{
							*reg2 = 1;
							*creg2 = '\0';
						}
						else if(c == 3)
						{
							*reg3 = 1;
							*creg3 = '\0';
						}
						else if(c == 4)
						{
							*reg4 = 1;
							*creg4 = '\0';
						}
						else if(c == 5)
						{
							*reg5 = 1;
							*creg5 = '\0';
						}
						else if(c == 6)
						{
							*reg6 = 1;
							*creg6 = '\0';
						}
						else if(c == 7)
						{
							*reg7 = 1;
							*creg7 = '\0';
						}
						else if(c == 8)
						{
							*reg8 = 1;
							*creg8 = '\0';
						}
						c++;
					}
				}
			}
			if(*op1 == ' ' || *op1 == '-' || *op1 == '+' || *op1 == '*')
                        	imm = 0;
			*op1++;
		}
		if(imm != 1 && entered == 1)
                {
			if(c == 1)
                        {
                        	*reg1 = 1;
                                *creg1 = '\0';
                        }
                        else if(c == 2)
                        {
                        	*reg2 = 1;
                                *creg2 = '\0';
                        }
                        else if(c == 3)
                        {
                        	*reg3 = 1;
                                *creg3 = '\0';
                        }
                        else if(c == 4)
                        {
                        	*reg4 = 1;
                                *creg4 = '\0';
                        }
                        else if(c == 5)
                        {
                        	*reg5 = 1;
                                *creg5 = '\0';
                        }
			else if(c == 6)
                        {
                        	*reg6 = 1;
                                *creg6 = '\0';
                        }
                        else if(c == 7)
                        {
                        	*reg7 = 1;
                        	*creg7 = '\0';
                        }
                        else if(c == 8)
                        {
                        	*reg8 = 1;
                        	*creg8 = '\0';
                        }
                        c++;
		}

	}
	else if(ptr == 1 || ptr == 2 || ptr == 3)
	{
		if(ptr == 1)
			addr += DWORDPTR_LENGTH;
		else if(ptr == 2)
			addr += BYTEPTR_LENGTH;
		else if(ptr == 3)
			addr += PTR_LENGTH;
		while( *addr != '\0' )
		{
			if(imm != 1)
                        {
				if(*addr == '0' || *addr == '1' || *addr == '2' || *addr == '3' || *addr == '4' || *addr == '5' || *addr == '6' || *addr == '7' || *addr == '8' || *addr == '9')
                                        imm = 1;
                                else
                                {
                                        if( *addr != ' ' && *addr != '+' && *addr != '-' && *addr != ']' && *addr != '*')
                                        {
						//printf("if %c\n",*addr);
                                                if(c == 1)
                                                        *creg1++ = *addr;
                                                else if(c == 2)
                                                        *creg2++ = *addr;
                                                else if(c == 3)
                                                        *creg3++ = *addr;
                                                else if(c == 4)
                                                        *creg4++ = *addr;
                                                else if(c == 5)
                                                        *creg5++ = *addr;
                                                else if(c == 6)
                                                        *creg6++ = *addr;
                                                else if(c == 7)
                                                        *creg7++ = *addr;
                                                else if(c == 8)
                                                        *creg8++ = *addr;
                                                else
                                                        printf("Error in reg breakdown\n");
                                        }
                                        else
                                        {
                                                if(c == 1)
                                                {
                                                        *reg1 = 1;
                                                        *creg1 = '\0';
                                                }
                                                else if(c == 2)
                                                {
                                                        *reg2 = 1;
                                                        *creg2 = '\0';
                                                }
                                                else if(c == 3)
                                                {
                                                        *reg3 = 1;
                                                        *creg3 = '\0';
                                                }
                                                else if(c == 4)
                                                {
                                                        *reg4 = 1;
                                                        *creg4 = '\0';
                                                }
                                                else if(c == 5)
                                                {
                                                        *reg5 = 1;
                                                        *creg5 = '\0';
                                                }
                                                else if(c == 6)
                                                {
                                                        *reg6 = 1;
                                                        *creg6 = '\0';
                                                }
                                                else if(c == 7)
                                                {
                                                        *reg7 = 1;
                                                        *creg7 = '\0';
                                                }
                                                else if(c == 8)
                                                {
                                                        *reg8 = 1;
                                                        *creg8 = '\0';
                                                }
                                                c++;
                                        }
                                }
                        }
			if(*addr == ' ' || *addr == '-' || *addr == '+' || *addr == ']' || *addr == '*')
                        	imm = 0;
			*addr++;
		} 
	}

	}

       	if(*reg1 == 1)
	{
		if(strlen(op2) > 0 || strcmp(mnem,"pop") == 0)
        		*reg1 = 2;
	}

	if(strlen(op2) > 0)
	{

	addr = NULL;
        ptr = 0;
        if( (addr = strstr(op2,DWORDPTR)) != NULL )
                ptr = 1;
        else if( (addr = strstr(op2,BYTEPTR)) != NULL )
                ptr = 2;
        else if( (addr = strstr(op2,PTR)) != NULL )
                ptr = 3;

        imm = 0;
        //No ptr, no indirect memory reference.
        if(ptr == 0)
        {
                int entered = 0;
                while( *op2 != '\0' )
                {
                        entered = 1;
                        if(imm != 1)
                        {
				if(*op2 == '0' || *op2 == '1' || *op2 == '2' || *op2 == '3' || *op2 == '4' || *op2 == '5' || *op2 == '6' || *op2 == '7' || *op2 == '8' || *op2 == '9')
                                        imm = 1;
                                else
                                {
                                        if(*op2 != ' ')
                                        {
                                                if(c == 1 )
                                                        *creg1++ = *op2;
                                                else if(c == 2)
                                                        *creg2++ = *op2;
                                                else if(c == 3)
                                                        *creg3++ = *op2;
                                                else if(c == 4)
                                                        *creg4++ = *op2;
                                                else if(c == 5)
                                                        *creg5++ = *op2;
                                                else if(c == 6)
                                                        *creg6++ = *op2;
                                                else if(c == 7)
                                                        *creg7++ = *op2;
                                                else if(c == 8)
                                                        *creg8++ = *op2;
                                                else
                                                        printf("Error in reg breakdown\n");
	 				}
                                        else
                                        {
                                                if(c == 1)
                                                {
                                                        *reg1 = 1;
                                                        *creg1 = '\0';
                                                }
                                                else if(c == 2)
                                                {
                                                        *reg2 = 1;
                                                        *creg2 = '\0';
                                                }
                                                else if(c == 3)
                                                {
                                                        *reg3 = 1;
                                                        *creg3 = '\0';
                                                }
                                                else if(c == 4)
                                                {
                                                        *reg4 = 1;
                                                        *creg4 = '\0';
                                                }
                                                else if(c == 5)
                                                {
                                                        *reg5 = 1;
                                                        *creg5 = '\0';
                                                }
                                                else if(c == 6)
                                                {
                                                        *reg6 = 1;
                                                        *creg6 = '\0';
                                                }
                                                else if(c == 7)
                                                {
                                                        *reg7 = 1;
                                                        *creg7 = '\0';
                                                }
                                                else if(c == 8)
                                                {
                                                        *reg8 = 1;
                                                        *creg8 = '\0';
                                                }
                                                c++;
                                        }
                                }
                        }
                        if(*op2 == ' ' || *op2 == '-' || *op2 == '+' || *op2 == '*')
                                imm = 0;
                        *op2++;
                }
 		if(imm != 1 && entered == 1)
                {
                        if(c == 1)
                        {
                                *reg1 = 1;
                                *creg1 = '\0';
                        }
                        else if(c == 2)
                        {
                                *reg2 = 1;
                                *creg2 = '\0';
                        }
                        else if(c == 3)
                        {
                                *reg3 = 1;
                                *creg3 = '\0';
                        }
                        else if(c == 4)
                        {
                                *reg4 = 1;
                                *creg4 = '\0';
                        }
                        else if(c == 5)
                        {
                                *reg5 = 1;
                                *creg5 = '\0';
                        }
                        else if(c == 6)
                        {
                                *reg6 = 1;
                                *creg6 = '\0';
                        }
                        else if(c == 7)
                        {
                                *reg7 = 1;
                                *creg7 = '\0';
                        }
                        else if(c == 8)
                        {
                                *reg8 = 1;
                                *creg8 = '\0';
                        }
                        c++;
                }

        }
	else if(ptr == 1 || ptr == 2 || ptr == 3)
        {
                if(ptr == 1)
                        addr += DWORDPTR_LENGTH;
                else if(ptr == 2)
                        addr += BYTEPTR_LENGTH;
                else if(ptr == 3)
                        addr += PTR_LENGTH;
                while( *addr != '\0' )
                {
                        if(imm != 1)
                        {
				if(*addr == '0' || *addr == '1' || *addr == '2' || *addr == '3' || *addr == '4' || *addr == '5' || *addr == '6' || *addr == '7' || *addr == '8' || *addr == '9')
                                        imm = 1;
                                else
                                {
                                        if( *addr != ' ' && *addr != '+' && *addr != '-' && *addr != ']' && *addr != '*')
                                        {
                                                //printf("if %c\n",*addr);
                                                if(c == 1)
                                                        *creg1++ = *addr;
                                                else if(c == 2)
                                                        *creg2++ = *addr;
                                                else if(c == 3)
                                                        *creg3++ = *addr;
                                                else if(c == 4)
                                                        *creg4++ = *addr;
                                                else if(c == 5)
                                                        *creg5++ = *addr;
                                                else if(c == 6)
                                                        *creg6++ = *addr;
                                                else if(c == 7)
                                                        *creg7++ = *addr;
                                                else if(c == 8)
                                                        *creg8++ = *addr;
                                                else
                                                        printf("Error in reg breakdown\n");
                                        }
					else
                                        {
                                                if(c == 1)
                                                {
                                                        *reg1 = 1;
                                                        *creg1 = '\0';
                                                }
                                                else if(c == 2)
                                                {
                                                        *reg2 = 1;
                                                        *creg2 = '\0';
                                                }
                                                else if(c == 3)
                                                {
                                                        *reg3 = 1;
                                                        *creg3 = '\0';
                                                }
                                                else if(c == 4)
                                                {
                                                        *reg4 = 1;
                                                        *creg4 = '\0';
                                                }
                                                else if(c == 5)
                                                {
                                                        *reg5 = 1;
                                                        *creg5 = '\0';
                                                }
                                                else if(c == 6)
                                                {
                                                        *reg6 = 1;
                                                        *creg6 = '\0';
                                                }
                                                else if(c == 7)
                                                {
                                                        *reg7 = 1;
                                                        *creg7 = '\0';
                                                }
                                                else if(c == 8)
                                                {
                                                        *reg8 = 1;
                                                        *creg8 = '\0';
                                                }
                                                c++;
                                        }
                                }
                        }
                        if(*addr == ' ' || *addr == '-' || *addr == '+' || *addr == ']' || *addr == '*')
                                imm = 0;
                        *addr++;
                }
        }
	
	}

	if(strlen(op3) > 0)
	{

	addr = NULL;
        ptr = 0;
        if( (addr = strstr(op3,DWORDPTR)) != NULL )
                ptr = 1;
        else if( (addr = strstr(op3,BYTEPTR)) != NULL )
                ptr = 2;
        else if( (addr = strstr(op3,PTR)) != NULL )
                ptr = 3;

        imm = 0;
        //No ptr, no indirect memory reference.
        if(ptr == 0)
        {
                int entered = 0;
                while( *op3 != '\0' )
                {
                        entered = 1;
                        if(imm != 1)
                        {
				if(*op3 == '0' || *op3 == '1' || *op3 == '2' || *op3 == '3' || *op3 == '4' || *op3 == '5' || *op3 == '6' || *op3 == '7' || *op3 == '8' || *op3 == '9')
                                        imm = 1;
                                else
                                {
                                        if(*op3 != ' ')
                                        {
                                                if(c == 1 )
                                                        *creg1++ = *op3;
                                                else if(c == 2)
                                                        *creg2++ = *op3;
                                                else if(c == 3)
                                                        *creg3++ = *op3;
                                                else if(c == 4)
                                                        *creg4++ = *op3;
                                                else if(c == 5)
                                                        *creg5++ = *op3;
                                                else if(c == 6)
                                                        *creg6++ = *op3;
                                                else if(c == 7)
                                                        *creg7++ = *op3;
                                                else if(c == 8)
                                                        *creg8++ = *op3;
                                                else
                                                        printf("Error in reg breakdown\n");
	 				}
                                        else
                                        {
                                                if(c == 1)
                                                {
                                                        *reg1 = 1;
                                                        *creg1 = '\0';
                                                }
                                                else if(c == 2)
                                                {
                                                        *reg2 = 1;
                                                        *creg2 = '\0';
                                                }
                                                else if(c == 3)
                                                {
                                                        *reg3 = 1;
                                                        *creg3 = '\0';
                                                }
                                                else if(c == 4)
                                                {
                                                        *reg4 = 1;
                                                        *creg4 = '\0';
                                                }
                                                else if(c == 5)
                                                {
                                                        *reg5 = 1;
                                                        *creg5 = '\0';
                                                }
                                                else if(c == 6)
                                                {
                                                        *reg6 = 1;
                                                        *creg6 = '\0';
                                                }
                                                else if(c == 7)
                                                {
                                                        *reg7 = 1;
                                                        *creg7 = '\0';
                                                }
                                                else if(c == 8)
                                                {
                                                        *reg8 = 1;
                                                        *creg8 = '\0';
                                                }
                                                c++;
                                        }
                                }
                        }
                        if(*op3 == ' ' || *op3 == '-' || *op3 == '+' || *op3 == '*')
                                imm = 0;
                        *op3++;
                }
 		if(imm != 1 && entered == 1)
                {
                        if(c == 1)
                        {
                                *reg1 = 1;
                                *creg1 = '\0';
                        }
                        else if(c == 2)
                        {
                                *reg2 = 1;
                                *creg2 = '\0';
                        }
                        else if(c == 3)
                        {
                                *reg3 = 1;
                                *creg3 = '\0';
                        }
                        else if(c == 4)
                        {
                                *reg4 = 1;
                                *creg4 = '\0';
                        }
                        else if(c == 5)
                        {
                                *reg5 = 1;
                                *creg5 = '\0';
                        }
                        else if(c == 6)
                        {
                                *reg6 = 1;
                                *creg6 = '\0';
                        }
                        else if(c == 7)
                        {
                                *reg7 = 1;
                                *creg7 = '\0';
                        }
                        else if(c == 8)
                        {
                                *reg8 = 1;
                                *creg8 = '\0';
                        }
                        c++;
                }

        }
	else if(ptr == 1 || ptr == 2 || ptr == 3)
        {
                if(ptr == 1)
                        addr += DWORDPTR_LENGTH;
                else if(ptr == 2)
                        addr += BYTEPTR_LENGTH;
                else if(ptr == 3)
                        addr += PTR_LENGTH;
                while( *addr != '\0' )
                {
                        if(imm != 1)
                        {
				if(*addr == '0' || *addr == '1' || *addr == '2' || *addr == '3' || *addr == '4' || *addr == '5' || *addr == '6' || *addr == '7' || *addr == '8' || *addr == '9')
                                        imm = 1;
                                else
                                {
                                        if( *addr != ' ' && *addr != '+' && *addr != '-' && *addr != ']' && *addr != '*')
                                        {
                                                //printf("if %c\n",*addr);
                                                if(c == 1)
                                                        *creg1++ = *addr;
                                                else if(c == 2)
                                                        *creg2++ = *addr;
                                                else if(c == 3)
                                                        *creg3++ = *addr;
                                                else if(c == 4)
                                                        *creg4++ = *addr;
                                                else if(c == 5)
                                                        *creg5++ = *addr;
                                                else if(c == 6)
                                                        *creg6++ = *addr;
                                                else if(c == 7)
                                                        *creg7++ = *addr;
                                                else if(c == 8)
                                                        *creg8++ = *addr;
                                                else
                                                        printf("Error in reg breakdown\n");
                                        }
					else
                                        {
                                                if(c == 1)
                                                {
                                                        *reg1 = 1;
                                                        *creg1 = '\0';
                                                }
                                                else if(c == 2)
                                                {
                                                        *reg2 = 1;
                                                        *creg2 = '\0';
                                                }
                                                else if(c == 3)
                                                {
                                                        *reg3 = 1;
                                                        *creg3 = '\0';
                                                }
                                                else if(c == 4)
                                                {
                                                        *reg4 = 1;
                                                        *creg4 = '\0';
                                                }
                                                else if(c == 5)
                                                {
                                                        *reg5 = 1;
                                                        *creg5 = '\0';
                                                }
                                                else if(c == 6)
                                                {
                                                        *reg6 = 1;
                                                        *creg6 = '\0';
                                                }
                                                else if(c == 7)
                                                {
                                                        *reg7 = 1;
                                                        *creg7 = '\0';
                                                }
                                                else if(c == 8)
                                                {
                                                        *reg8 = 1;
                                                        *creg8 = '\0';
                                                }
                                                c++;
                                        }
                                }
                        }
                        if(*addr == ' ' || *addr == '-' || *addr == '+' || *addr == ']' || *addr == '*')
                                imm = 0;
                        *addr++;
                }
        }
	
	}


	if(strlen(op4) > 0)
	{

	addr = NULL;
        ptr = 0;
        if( (addr = strstr(op4,DWORDPTR)) != NULL )
                ptr = 1;
        else if( (addr = strstr(op4,BYTEPTR)) != NULL )
                ptr = 2;
        else if( (addr = strstr(op4,PTR)) != NULL )
                ptr = 3;

        imm = 0;
        //No ptr, no indirect memory reference.
        if(ptr == 0)
        {
                int entered = 0;
                while( *op4 != '\0' )
                {
                        entered = 1;
                        if(imm != 1)
                        {
				if(*op4 == '0' || *op4 == '1' || *op4 == '2' || *op4 == '3' || *op4 == '4' || *op4 == '5' || *op4 == '6' || *op4 == '7' || *op4 == '8' || *op4 == '9')
                                        imm = 1;
                                else
                                {
                                        if(*op4 != ' ')
                                        {
                                                if(c == 1 )
                                                        *creg1++ = *op4;
                                                else if(c == 2)
                                                        *creg2++ = *op4;
                                                else if(c == 3)
                                                        *creg3++ = *op4;
                                                else if(c == 4)
                                                        *creg4++ = *op4;
                                                else if(c == 5)
                                                        *creg5++ = *op4;
                                                else if(c == 6)
                                                        *creg6++ = *op4;
                                                else if(c == 7)
                                                        *creg7++ = *op4;
                                                else if(c == 8)
                                                        *creg8++ = *op4;
                                                else
                                                        printf("Error in reg breakdown\n");
	 				}
                                        else
                                        {
                                                if(c == 1)
                                                {
                                                        *reg1 = 1;
                                                        *creg1 = '\0';
                                                }
                                                else if(c == 2)
                                                {
                                                        *reg2 = 1;
                                                        *creg2 = '\0';
                                                }
                                                else if(c == 3)
                                                {
                                                        *reg3 = 1;
                                                        *creg3 = '\0';
                                                }
                                                else if(c == 4)
                                                {
                                                        *reg4 = 1;
                                                        *creg4 = '\0';
                                                }
                                                else if(c == 5)
                                                {
                                                        *reg5 = 1;
                                                        *creg5 = '\0';
                                                }
                                                else if(c == 6)
                                                {
                                                        *reg6 = 1;
                                                        *creg6 = '\0';
                                                }
                                                else if(c == 7)
                                                {
                                                        *reg7 = 1;
                                                        *creg7 = '\0';
                                                }
                                                else if(c == 8)
                                                {
                                                        *reg8 = 1;
                                                        *creg8 = '\0';
                                                }
                                                c++;
                                        }
                                }
                        }
                        if(*op4 == ' ' || *op4 == '-' || *op4 == '+' || *op4 == '*')
                                imm = 0;
                        *op4++;
                }
 		if(imm != 1 && entered == 1)
                {
                        if(c == 1)
                        {
                                *reg1 = 1;
                                *creg1 = '\0';
                        }
                        else if(c == 2)
                        {
                                *reg2 = 1;
                                *creg2 = '\0';
                        }
                        else if(c == 3)
                        {
                                *reg3 = 1;
                                *creg3 = '\0';
                        }
                        else if(c == 4)
                        {
                                *reg4 = 1;
                                *creg4 = '\0';
                        }
                        else if(c == 5)
                        {
                                *reg5 = 1;
                                *creg5 = '\0';
                        }
                        else if(c == 6)
                        {
                                *reg6 = 1;
                                *creg6 = '\0';
                        }
                        else if(c == 7)
                        {
                                *reg7 = 1;
                                *creg7 = '\0';
                        }
                        else if(c == 8)
                        {
                                *reg8 = 1;
                                *creg8 = '\0';
                        }
                        c++;
                }

        }
	else if(ptr == 1 || ptr == 2 || ptr == 3)
        {
                if(ptr == 1)
                        addr += DWORDPTR_LENGTH;
                else if(ptr == 2)
                        addr += BYTEPTR_LENGTH;
                else if(ptr == 3)
                        addr += PTR_LENGTH;
                while( *addr != '\0' )
                {
                        if(imm != 1)
                        {
				if(*addr == '0' || *addr == '1' || *addr == '2' || *addr == '3' || *addr == '4' || *addr == '5' || *addr == '6' || *addr == '7' || *addr == '8' || *addr == '9')
                                        imm = 1;
                                else
                                {
                                        if( *addr != ' ' && *addr != '+' && *addr != '-' && *addr != ']' && *addr != '*')
                                        {
                                                //printf("if %c\n",*addr);
                                                if(c == 1)
                                                        *creg1++ = *addr;
                                                else if(c == 2)
                                                        *creg2++ = *addr;
                                                else if(c == 3)
                                                        *creg3++ = *addr;
                                                else if(c == 4)
                                                        *creg4++ = *addr;
                                                else if(c == 5)
                                                        *creg5++ = *addr;
                                                else if(c == 6)
                                                        *creg6++ = *addr;
                                                else if(c == 7)
                                                        *creg7++ = *addr;
                                                else if(c == 8)
                                                        *creg8++ = *addr;
                                                else
                                                        printf("Error in reg breakdown\n");
                                        }
					else
                                        {
                                                if(c == 1)
                                                {
                                                        *reg1 = 1;
                                                        *creg1 = '\0';
                                                }
                                                else if(c == 2)
                                                {
                                                        *reg2 = 1;
                                                        *creg2 = '\0';
                                                }
                                                else if(c == 3)
                                                {
                                                        *reg3 = 1;
                                                        *creg3 = '\0';
                                                }
                                                else if(c == 4)
                                                {
                                                        *reg4 = 1;
                                                        *creg4 = '\0';
                                                }
                                                else if(c == 5)
                                                {
                                                        *reg5 = 1;
                                                        *creg5 = '\0';
                                                }
                                                else if(c == 6)
                                                {
                                                        *reg6 = 1;
                                                        *creg6 = '\0';
                                                }
                                                else if(c == 7)
                                                {
                                                        *reg7 = 1;
                                                        *creg7 = '\0';
                                                }
                                                else if(c == 8)
                                                {
                                                        *reg8 = 1;
                                                        *creg8 = '\0';
                                                }
                                                c++;
                                        }
                                }
                        }
                        if(*addr == ' ' || *addr == '-' || *addr == '+' || *addr == ']' || *addr == '*')
                                imm = 0;
                        *addr++;
                }
        }
	
	}

	if(strlen(op5) > 0)
	{

	addr = NULL;
        ptr = 0;
        if( (addr = strstr(op5,DWORDPTR)) != NULL )
                ptr = 1;
        else if( (addr = strstr(op5,BYTEPTR)) != NULL )
                ptr = 2;
        else if( (addr = strstr(op5,PTR)) != NULL )
                ptr = 3;

        imm = 0;
        //No ptr, no indirect memory reference.
        if(ptr == 0)
        {
                int entered = 0;
                while( *op5 != '\0' )
                {
                        entered = 1;
                        if(imm != 1)
                        {
				if(*op5 == '0' || *op5 == '1' || *op5 == '2' || *op5 == '3' || *op5 == '4' || *op5 == '5' || *op5 == '6' || *op5 == '7' || *op5 == '8' || *op5 == '9')
                                        imm = 1;
                                else
                                {
                                        if(*op5 != ' ')
                                        {
                                                if(c == 1 )
                                                        *creg1++ = *op5;
                                                else if(c == 2)
                                                        *creg2++ = *op5;
                                                else if(c == 3)
                                                        *creg3++ = *op5;
                                                else if(c == 4)
                                                        *creg4++ = *op5;
                                                else if(c == 5)
                                                        *creg5++ = *op5;
                                                else if(c == 6)
                                                        *creg6++ = *op5;
                                                else if(c == 7)
                                                        *creg7++ = *op5;
                                                else if(c == 8)
                                                        *creg8++ = *op5;
                                                else
                                                        printf("Error in reg breakdown\n");
	 				}
                                        else
                                        {
                                                if(c == 1)
                                                {
                                                        *reg1 = 1;
                                                        *creg1 = '\0';
                                                }
                                                else if(c == 2)
                                                {
                                                        *reg2 = 1;
                                                        *creg2 = '\0';
                                                }
                                                else if(c == 3)
                                                {
                                                        *reg3 = 1;
                                                        *creg3 = '\0';
                                                }
                                                else if(c == 4)
                                                {
                                                        *reg4 = 1;
                                                        *creg4 = '\0';
                                                }
                                                else if(c == 5)
                                                {
                                                        *reg5 = 1;
                                                        *creg5 = '\0';
                                                }
                                                else if(c == 6)
                                                {
                                                        *reg6 = 1;
                                                        *creg6 = '\0';
                                                }
                                                else if(c == 7)
                                                {
                                                        *reg7 = 1;
                                                        *creg7 = '\0';
                                                }
                                                else if(c == 8)
                                                {
                                                        *reg8 = 1;
                                                        *creg8 = '\0';
                                                }
                                                c++;
                                        }
                                }
                        }
                        if(*op5 == ' ' || *op5 == '-' || *op5 == '+' || *op5 == '*')
                                imm = 0;
                        *op5++;
                }
 		if(imm != 1 && entered == 1)
                {
                        if(c == 1)
                        {
                                *reg1 = 1;
                                *creg1 = '\0';
                        }
                        else if(c == 2)
                        {
                                *reg2 = 1;
                                *creg2 = '\0';
                        }
                        else if(c == 3)
                        {
                                *reg3 = 1;
                                *creg3 = '\0';
                        }
                        else if(c == 4)
                        {
                                *reg4 = 1;
                                *creg4 = '\0';
                        }
                        else if(c == 5)
                        {
                                *reg5 = 1;
                                *creg5 = '\0';
                        }
                        else if(c == 6)
                        {
                                *reg6 = 1;
                                *creg6 = '\0';
                        }
                        else if(c == 7)
                        {
                                *reg7 = 1;
                                *creg7 = '\0';
                        }
                        else if(c == 8)
                        {
                                *reg8 = 1;
                                *creg8 = '\0';
                        }
                        c++;
                }

        }
	else if(ptr == 1 || ptr == 2 || ptr == 3)
        {
                if(ptr == 1)
                        addr += DWORDPTR_LENGTH;
                else if(ptr == 2)
                        addr += BYTEPTR_LENGTH;
                else if(ptr == 3)
                        addr += PTR_LENGTH;
                while( *addr != '\0' )
                {
                        if(imm != 1)
                        {
				if(*addr == '0' || *addr == '1' || *addr == '2' || *addr == '3' || *addr == '4' || *addr == '5' || *addr == '6' || *addr == '7' || *addr == '8' || *addr == '9')
                                        imm = 1;
                                else
                                {
                                        if( *addr != ' ' && *addr != '+' && *addr != '-' && *addr != ']' && *addr != '*')
                                        {
                                                //printf("if %c\n",*addr);
                                                if(c == 1)
                                                        *creg1++ = *addr;
                                                else if(c == 2)
                                                        *creg2++ = *addr;
                                                else if(c == 3)
                                                        *creg3++ = *addr;
                                                else if(c == 4)
                                                        *creg4++ = *addr;
                                                else if(c == 5)
                                                        *creg5++ = *addr;
                                                else if(c == 6)
                                                        *creg6++ = *addr;
                                                else if(c == 7)
                                                        *creg7++ = *addr;
                                                else if(c == 8)
                                                        *creg8++ = *addr;
                                                else
                                                        printf("Error in reg breakdown\n");
                                        }
					else
                                        {
                                                if(c == 1)
                                                {
                                                        *reg1 = 1;
                                                        *creg1 = '\0';
                                                }
                                                else if(c == 2)
                                                {
                                                        *reg2 = 1;
                                                        *creg2 = '\0';
                                                }
                                                else if(c == 3)
                                                {
                                                        *reg3 = 1;
                                                        *creg3 = '\0';
                                                }
                                                else if(c == 4)
                                                {
                                                        *reg4 = 1;
                                                        *creg4 = '\0';
                                                }
                                                else if(c == 5)
                                                {
                                                        *reg5 = 1;
                                                        *creg5 = '\0';
                                                }
                                                else if(c == 6)
                                                {
                                                        *reg6 = 1;
                                                        *creg6 = '\0';
                                                }
                                                else if(c == 7)
                                                {
                                                        *reg7 = 1;
                                                        *creg7 = '\0';
                                                }
                                                else if(c == 8)
                                                {
                                                        *reg8 = 1;
                                                        *creg8 = '\0';
                                                }
                                                c++;
                                        }
                                }
                        }
                        if(*addr == ' ' || *addr == '-' || *addr == '+' || *addr == ']' || *addr == '*')
                                imm = 0;
                        *addr++;
                }
        }
	
	}

	return 0;
}

int get_insaddress(const char *line, char *addr)
{
	char *address = NULL;
	if( (address = strstr(line,ADDRESS)) == NULL )
		return -1;
	address += ADDRESS_LENGTH;
	while( (*address != ' ') && (*address != '\0') )
		*addr++ = *address++;
	*addr = '\0';
	return 0;
}

int get_readregmax(const char *line)
{
	char *addr = (char *)malloc (5);
	char *begin = addr;
	char *address = NULL;
	if( (address = strstr(line,READREGMAX)) == NULL )
		return -1;
	address += READREGMAX_LENGTH;
	while( (*address != ' ') && (*address != '\0') )
		*addr++ = *address++;
	*addr = '\0';
	int rmax = atoi(addr);
	free(begin);
	return rmax;
}
int get_writeregmax(const char *line)
{
	char *addr = (char *)malloc (5);
	char *begin = addr;
	char *address = NULL;
	if( (address = strstr(line,WRITEREGMAX)) == NULL )
		return -1;
	address += WRITEREGMAX_LENGTH;
	while( (*address != ' ') && (*address != '\0') )
		*addr++ = *address++;
	*addr = '\0';
	int wmax = atoi(addr);
	free(begin);
	return wmax;
}
int get_memcount(const char *line)
{
	char *addr = (char *)malloc (5);
	char *begin = addr;
	char *address = NULL;
	if( (address = strstr(line,MEMCOUNT)) == NULL )
		return -1;
	address += MEMCOUNT_LENGTH;
	while( (*address != ' ') && (*address != '\0') )
		*addr++ = *address++;
	*addr = '\0';
	int mcount = atoi(addr);
	free(begin);
	return mcount;
}
